****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************

clear all 
set more off

**CREATE TABLE 5

cd $data
use temp_main.dta, clear
by f2000, sort: egen min_test = min(lag_avg_test)

xtile nqtestscores = min_test, nq(4)
quietly tab nqtestscores, gen(TSgroups)
foreach var of varlist TSgroups1-TSgroups4 {
gen `var'_TS = `var' * lag_avg_test
}



global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park i.storyheigh i.aheatingty i.heatedfuel i.actype i.extwall i.foundation i.bldggrade  CBGPopDensity CBGMedianHHIncome PercUnemploy"
xi:reg LogPrice lag_avg_test $covars1 , r
 est store est1
estadd scalar N_ind =e(N) 
estadd scalar Rsq =e(r2) 
estadd local fe1="-"
estadd local fe2="-"
estadd local fe3="-" 


global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park i.storyheigh i.aheatingty i.heatedfuel i.actype i.extwall i.foundation i.bldggrade  CBGPopDensity CBGMedianHHIncome PercUnemploy"
xi:areg LogPrice lag_avg_test $covars1 , abs(t2000) r
 est store est2
estadd scalar N_ind =e(N) 
estadd scalar Rsq =e(r2)  
estadd local fe1="\checkmark"
estadd local fe2="-"
estadd local fe3="-"

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park i.storyheigh i.aheatingty i.heatedfuel i.actype i.extwall i.foundation i.bldggrade "
xi:areg LogPrice lag_avg_test $covars1 , abs(f2000) r
 est store est3
estadd scalar N_ind =e(N) 
estadd scalar Rsq =e(r2)  
estadd local fe1="-"
estadd local fe2="\checkmark"
estadd local fe3="-"

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park i.storyheigh i.aheatingty i.heatedfuel i.actype i.extwall i.foundation i.bldggrade "
xi:areg LogPrice lag_avg_test $covars1 , abs(b2000) r
 est store est4
estadd scalar N_ind =e(N) 
estadd scalar Rsq =e(r2)  
estadd local fe1="-"
estadd local fe2="-"
estadd local fe3="\checkmark"


global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park i.storyheigh i.aheatingty i.heatedfuel i.actype i.extwall i.foundation i.bldggrade "

*global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park i.storyheigh i.aheatingty i.heatedfuel i.actype i.extwall i.foundation i.bldggrade  CBGPopDensity CBGMedianHHIncome PercUnemploy"
xi:areg LogPrice lag_avg_test TSgroups2_TS TSgroups3_TS TSgroups4_TS $covars1 , abs(f2000) r
 est store est5
estadd scalar N_ind =e(N) 
estadd scalar Rsq =e(r2)  
estadd local fe1="-"
estadd local fe2="\checkmark"
estadd local fe3="-"



label var lag_avg_test "Avg. Elem. Test Scores (t-1)"
label var TSgroups2_TS "Avg. Elem. Test Scores (t-1)*2nd Qt. Neigh Test Scores"
label var TSgroups3_TS "Avg. Elem. Test Scores (t-1)*3rd Qt. Neigh Test Scores"
label var TSgroups4_TS "Avg. Elem. Test Scores (t-1)*4th Qt. Neigh Test Scores"


 esttab  est1 est2 est3 est4 est5  using table5.tex, replace f ///
	label booktabs b(4) p(4) eqlabels(none) alignment(S) nodepvars collabels(none) noobs ///
	mtitles( "Log(Price)" "Log(Price)" "Log(Price)" "Log(Price)"  ///
	"Log(Price)" ) nonumbers keep(lag_avg_test TSgroups2_TS TSgroups3_TS TSgroups4_TS) ///
	refcat( "" , nolabel) ///
	star(* 0.10 ** 0.05 *** 0.01) compress ///
	nogaps lines width(\hsize) ///
	cells("b(fmt(4)star)" "se(fmt(4)par)") ///
		stats(Rsq N_ind fe1 fe2 fe3  , fmt(%9.4fc %9.0fc %1s %1s %1s  ) ///
	layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") ///
	labels(`"R-squared"' `"Observations"' `"Tract FEs"' `"CBG FEs"' `"Block FEs"' )) 
